package cn.daniellee.plugin.ice;

import cn.daniellee.plugin.ice.command.BaoxueCommand;
import cn.daniellee.plugin.ice.command.BaoxueCompleter;
import cn.daniellee.plugin.ice.model.PlayerData;
import cn.daniellee.plugin.ice.runnable.BaoxueRunnable;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class IceBaoxue extends JavaPlugin {

    private static final String PACKAGE_VERSION = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];

    private static IceBaoxue instance;

    private String prefix;

    private BukkitTask particleTask;

    private File playerDataFile = new File(this.getDataFolder(), "player.yml");

    private FileConfiguration playerDataYaml = new YamlConfiguration();

    public Map<String, PlayerData> playerData = new HashMap<>();

    public Map <String, Integer> suctionValue = new HashMap<>();

    public void onEnable(){
        instance = this;

        loadConfig();
        getLogger().info(" ");
        getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> ICE Vape Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
        getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
        getLogger().info(" ");

        Bukkit.getPluginCommand("ice").setExecutor(new BaoxueCommand());
        Bukkit.getPluginCommand("ice").setTabCompleter(new BaoxueCompleter());

        particleTask = new BaoxueRunnable().runTaskTimer(this, 0, 20);
    }

    public void loadConfig() {
        getLogger().info("[ICE Vape] Loading config...");
        if(!getDataFolder().exists()) getDataFolder().mkdirs();
        prefix = "&7[&b" + getConfig().get("prompt-prefix", "ICE Vape") + "&7] &3: &r";
        saveDefaultConfig();
        suctionValue.put("s", getConfig().getInt("setting.small-suction", 30));
        suctionValue.put("m", getConfig().getInt("setting.medium-suction", 60));
        suctionValue.put("l", getConfig().getInt("setting.large-suction", 90));
        suctionValue.put("v", getConfig().getInt("setting.violent-suction", 1500));
        try {
            if (!playerDataFile.exists()) playerDataFile.createNewFile();
            playerDataYaml.load(playerDataFile);
        } catch (Exception e) {
            this.getLogger().info(" ");
            this.getLogger().info("[ICE Vape]An error occurred in player file load.".replace("&", "§"));
            this.getLogger().info(" ");
            e.printStackTrace();
        }
        Set<String> names = playerDataYaml.getKeys(false);
        for (String name : names) {
            PlayerData data = new PlayerData(name);
            data.setEnable(playerDataYaml.getBoolean(name + ".enable"));
            data.setPeriod(playerDataYaml.getInt(name + ".period", getConfig().getInt("setting.default-period", 60)));
            data.setSuction(playerDataYaml.getString(name + ".suction", getConfig().getString("setting.default-suction", "m")));
            playerData.put(name, data);
        }
    }

    @Override
    public void onDisable() {
        if (particleTask != null) particleTask.cancel();

        getLogger().info(" ");
        getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> ICE Vape Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
        getLogger().info(" ");
    }

    public static IceBaoxue getInstance() {
        return instance;
    }

    public String getPrefix() {
        return prefix;
    }

    public void updatePlayerDataYaml(String key, Object value) {
        playerDataYaml.set(key, value);
        try {
            playerDataYaml.save(playerDataFile);
        } catch (IOException e) {
            this.getLogger().info(" ");
            this.getLogger().info("[ICE Vape]An error occurred in player file save.".replace("&", "§"));
            this.getLogger().info(" ");
            e.printStackTrace();
        }
    }

    public static Class<?> getNMSClass(String nmsClassName) throws ClassNotFoundException {
		return Class.forName("net.minecraft.server." + PACKAGE_VERSION + "." + nmsClassName);
	}
}
